# pylint: disable-msg=W0232
"""
@author: Sergiy Tsymbalyuk
"""

from pylons.i18n import lazy_ugettext as _

from sqlalchemy import Table, Column, ForeignKey
from sqlalchemy.types import Integer, String, Date, DateTime, Text, Boolean
from sqlalchemy.orm import relation, backref

from catalogue.model.meta import Base
from chowa.model.auth import User
from chowa.model.organization import Institution

__all__ = ['DocumentType',
           'Document',
           'File',
           'Tag',
           'document_tags',
           'Section',
           'TopSection',
           'SubSection',
           'document_sections',
           'user_sections',
           'user_favorites']

#### DOCUMENT TYPES ####

class DocumentType(Base):
    __tablename__ = 'catalogue_document_type'
    
    id   = Column(Integer, primary_key=True)
    name = Column(String(20), info={'verbose':_('Object|Name')})
    has_number = Column(Boolean)
    
    def __unicode__(self):
        return self.name

#### DOCUMENT ####

class Document(Base):
    __tablename__ = 'catalogue_document'
    __mapper_args__ = {'order_by': 'created desc'}
    
    id = Column(Integer, primary_key=True)
    
    title    = Column(String(255), info={'verbose':_('Title')})
    number   = Column(String(20), info={'verbose':_('Number')})
    approved = Column(Date, info={'verbose':_('Approved')})
    
    author_id      = Column(Integer, ForeignKey(User.id), info={'verbose':_('Author')})
    type_id        = Column(Integer, ForeignKey(DocumentType.id), info={'verbose':_('Type')})
    institution_id = Column(Integer, ForeignKey(Institution.id), info={'verbose':_('Institution')})
    
    created  = Column(DateTime)
    modified = Column(DateTime)
    
    author      = relation(User, backref='documents')
    type        = relation(DocumentType, backref='documents')
    tags        = relation('Tag', secondary='catalogue_document_tag', backref='documents')
    sections    = relation('Section', secondary='catalogue_document_section', backref='documents')
    institution = relation(Institution, backref='documents')
    who_like    = relation(User, secondary='catalogue_favorite')
    
    def __unicode__(self):
        return u'%s %s #%s %s' % (self.type or '',
                                  self.institution or '',
                                  self.number or '',
                                  self.approved or '')


class File(Base):
    __tablename__ = 'catalogue_file'
    
    id = Column(Integer, primary_key=True)
    
    original_name = Column(String(255), info={'verbose':_('Download name')}) # original filename (for download)
    stored_name   = Column(String(255), info={'verbose':_('Upload name')}) # filename on the server (can be hashed)
    description   = Column(Text, info={'verbose':_('Description')})
    document_id   = Column(Integer, ForeignKey(Document.id), info={'verbose':_('Document')})
    
    document = relation(Document, backref='files')
    
    def __unicode__(self):
        return self.original_name
    
#### TAGS ####

class Tag(Base):
    __tablename__ = 'catalogue_tag'
    __mapper_args__ = {'order_by': 'name'}
    
    id   = Column(Integer, primary_key=True)
    name = Column(String(20), info={'verbose':_('Object|Name')})
    
    def __unicode__(self):
        return self.name


document_tags = Table('catalogue_document_tag', Base.metadata,
    Column('document_id', Integer,
           ForeignKey(Document.id), primary_key=True),
    Column('tag_id', Integer,
           ForeignKey(Tag.id), primary_key=True))

#### SECTIONS ####

class Section(Base):
    __tablename__ = 'catalogue_section'
    
    id = Column(Integer, primary_key=True)
    name = Column(String(255), info={'verbose':_('Object|Name')})
    subsections = relation('SubSection', backref=backref('parent', remote_side='Section.id'))
    
    is_top = Column(Boolean, info={'verbose':_('Is top')})
    __mapper_args__ = {
        'polymorphic_on': is_top,
        'order_by': 'name',
    }
    
    def __unicode__(self):
        return self.name

class TopSection(Section):
    __mapper_args__ = {'polymorphic_identity': True}

class SubSection(Section):
    __mapper_args__ = {'polymorphic_identity': False}
    parent_id = Column(Integer, ForeignKey(Section.id), info={'verbose':_('Parent')})


document_sections = Table('catalogue_document_section', Base.metadata,
    Column('document_id', Integer,
           ForeignKey(Document.id), primary_key=True),
    Column('section_id', Integer,
           ForeignKey(Section.id), primary_key=True))

user_sections = Table('catalogue_user_section', Base.metadata,
    Column('user_id', Integer,
           ForeignKey(User.id), primary_key=True),
    Column('section_id', Integer,
           ForeignKey(Section.id), primary_key=True))

User.sections = relation(Section, secondary=user_sections)

#### FAVORITES ####

user_favorites = Table('catalogue_favorite', Base.metadata,
    Column('user_id', Integer,
           ForeignKey(User.id), primary_key=True),
    Column('document_id', Integer,
           ForeignKey(Document.id), primary_key=True))
